package com.itheima.service.backend.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.entity.PageResult;
import com.itheima.mapper.backend.CheckGroupCheckItemMapper;
import com.itheima.mapper.backend.CheckGroupMapper;
import com.itheima.mapper.backend.CheckMapper;
import com.itheima.mapper.backend.SetMealCheckGroupMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.backend.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    CheckGroupMapper checkGroupMapper;
    @Autowired
    CheckGroupCheckItemMapper checkGroupCheckItemMapper;

    @Autowired
    SetMealCheckGroupMapper setMealCheckGroupMapper;
    @Override
    public void deleteById(Integer id) {
        setMealCheckGroupMapper.deleteByCheckGroupId(id);
        checkGroupCheckItemMapper.deleteByCheckGroupId(id);
        checkGroupMapper.deleteById(id);

    }

    @Override
    public List<Integer> findCheckGroupIdsBySetmealId(Integer setmealId) {
        if (setmealId==null){
            return null;
        }
        List<Integer> checkGroupIds =setMealCheckGroupMapper.findCheckGroupIdsBySetmealId(setmealId);
        List<Integer> checkItems=checkGroupCheckItemMapper.findCheckItemIdsByCheckGroupIds(checkGroupIds);
        return checkItems;
    }

    @Override
    public PageResult page(Integer page, Integer pageSize, String name) {
        //1. 设置分页参数
        PageHelper.startPage(page,pageSize);

        //2. 执行查询
        List<CheckGroup> checkGroupList = checkGroupMapper.list(name);
        Page<CheckGroup> pageBean = (Page<CheckGroup>) checkGroupList;


        //3. 封装PageBean对象
        PageResult pageResult = new PageResult(pageBean.getTotal(),pageBean.getResult());
        return pageResult;
    }

    @Override
    public List<CheckGroup> list() {
        return checkGroupMapper.findAll();
    }

    @Override
    public CheckGroup findById(Integer id) {
        return checkGroupMapper.findById(id);
    }
}
